﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.Resources;
using System.Text.RegularExpressions;
using System.Threading;

namespace Framework.Data.Utilities
{
    public class MyResource
    {
        #region "Membres"
        //Instance du ResourceManager
        protected static ResourceManager _instance;
        #endregion

        #region "Méthodes"

        //Cette fonction retourne une instance d'un ResourceManager
        private static ResourceManager GetResourceManager(string ressourcesPath = null)
        {

            if (_instance == null)
            {
                if (string.IsNullOrEmpty(ressourcesPath))
                {
                    _instance = ResourceManager.CreateFileBasedResourceManager("ressource", System.Reflection.Assembly.GetExecutingAssembly().Location + "\\Ressources", null);
                }
                else
                {
                    _instance = ResourceManager.CreateFileBasedResourceManager("ressource", ressourcesPath + "\\Ressources", null);
                }
            }

            return _instance;

        }

        //Change la culture pour l'affichage et la manipulation des Strings
        public static void ChangeCulture(string nomCulture, string ressourcesPath = null)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(nomCulture);
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(nomCulture);
            
            //On force le format de date pour l'Application
            Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
            Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";

            //On force le format des nombres pour l'application
            Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator = ".";
            Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyGroupSeparator = ",";
            Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";
            Thread.CurrentThread.CurrentCulture.NumberFormat.NumberGroupSeparator = ",";

            Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator = ".";
            Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyGroupSeparator = ",";
            Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator = ".";
            Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberGroupSeparator = ",";


            SetResourceManager(ressourcesPath);
        }

        //Assigne un ResourceManager
        private static void SetResourceManager(string ressourcesPath = null)
        {
            if (string.IsNullOrEmpty(ressourcesPath))
            {
                _instance = ResourceManager.CreateFileBasedResourceManager("ressource", System.Reflection.Assembly.GetExecutingAssembly().Location + "\\Ressources", null);
            }
            else
            {
                _instance = ResourceManager.CreateFileBasedResourceManager("ressource", ressourcesPath + "\\Ressources", null);
            }
        }

        /// <summary>
        /// Recherche une string dans le fichier de ressource et le retourne.
        /// </summary>
        /// <param name="nom">Nom à rechercher</param>
        /// <returns>String</returns>
        /// <remarks></remarks>
        public static string GetString(string nom, string ressourcesPath = null)
        {
            List<string> result = new List<string>();
            MatchCollection mc = null;
            Regex regx = new Regex("(\\<\\#[A-Za-z0-9_.éÉèÈàÀ]+\\#\\>)");
            List<string> chaines = new List<string>();
            try
            {
                //Si le nom est vide, on retourne une chaîne vide
                if ((nom == null) || (nom.Length == 0))
                    return "";
                chaines.AddRange(nom.Split(' '));
                foreach (string chaine in chaines)
                {
                    result.Add(GetResourceManager(ressourcesPath).GetString(chaine.ToUpper()));
                    mc = regx.Matches(result[result.Count - 1]);
                    if (mc.Count > 0)
                    {
                        for (Int32 i = 0; i <= mc.Count - 1; i++)
                        {
                            result[result.Count - 1] = result[result.Count - 1].Replace(mc[i].Value, MyResource.GetString(mc[i].Value.Substring(2, mc[i].Value.Length - 4).ToUpper()));
                        }
                    }
                }
                return string.Join(" ", result.ToArray());
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Fail("La ressource [" + nom + "] n'a pas été trouvé dans le fichier de ressource.", ex.Message);
                return "[Non trouvé]";
            }
        }

        /// <summary>
        /// Fonction qui retourn la langue courrante
        /// </summary>
        /// <returns>string</returns>
        /// <remarks></remarks>
        public static string GetLanguage()
        {
            string language = "Français";

            if (System.Threading.Thread.CurrentThread.CurrentUICulture.Name.Substring(0, 2).Equals("EN", StringComparison.InvariantCultureIgnoreCase))
            {
                language = "English";
            }
            return language;
        }

        #endregion

    }
}
